package edu.ccut.saturn.studio.dialog;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;

public class MyCellModifierParameterFromWorkflow implements ICellModifier {
	
	private TableViewer tableViewer;
	
	public MyCellModifierParameterFromWorkflow(TableViewer tableViewer){
		this.tableViewer = tableViewer;
	}

	public boolean canModify(Object element, String property) {
		return true;
	}

	public Object getValue(Object element, String property) {
		SaturnParameter o = (SaturnParameter) element;
		if (property.equals("id")) {
			return o.getId();
		} else if (property.equals("name")) {
			return o.getName();
		} else if (property.equals("classType")) {
			return getClassTypeIndex(o.getClassType());
		} else if (property.equals("passingType")){
			return getPassingTypeIndex(o.getPassingType());
		} else if (property.equals("valueKey")){
			return o.getValueKey();
		} else if (property.equals("scope")){
			return getScopeIndex(o.getScope());
		} else if (property.equals("description")){
			return o.getDescription();
		}
			throw new RuntimeException("" + property); 
	}
	
	private int getClassTypeIndex(String classType) {
		for (int i = 0; i < SelectionCandidate.classtypes.length; i++) {
			if (SelectionCandidate.classtypes[i].equals(classType))
				return i;
		}
		return -1;
	}
	
	private int getPassingTypeIndex(String passingType) {
		for (int i = 0; i < SelectionCandidate.passingTypes.length; i++) {
			if (SelectionCandidate.passingTypes[i].equals(passingType))
				return i;
		}
		return -1;
	}
	
	private int getScopeIndex(String scope) {
		for (int i = 0; i < SelectionCandidate.scopes.length; i++) {
			if (SelectionCandidate.scopes[i].equals(scope))
				return i;
		}
		return -1;
	}

	public void modify(Object element, String property, Object value) {
		TableItem item = (TableItem) element;
		SaturnParameter o = (SaturnParameter) item.getData();
		
		if(property.equals("valueKey")){
			String newValueKey = (String) value;
			o.setValueKey(newValueKey);
		} else {
			throw new RuntimeException("" + property); 
		}
		tableViewer.update(o, null);
	}
}
